<template>
    <van-list
      class="comment-list"
      v-model="loading"
      :finished="finished"
      finished-text="已显示全部评论"
      @load="onLoad"
    >
    <van-cell title="全部回复"></van-cell>
    <commentItem
      v-for="(item,index) in list"
      :key="index"
      :comment="item"
      :immediate-check="false"
      @update-is_liking="item.is_liking=$event"
      @replyClick="$emit('replyClick',$event)"
    />
    </van-list>
</template>

<script>
import { getComments } from '@/api/comment'
import commentItem from '../comment-item'
export default {
  name: 'commentList',
  components: {
    commentItem
  },
  props: {
    source: {
      type: [String, Number, Object],
      required: true
    },
    list: {
      type: Array,
      default: () => []
    },
    type: {
      type: String,
      validator (val) {
        return ['a', 'c'].includes(val)
      },
      default: () => 'a'
    }
  },
  data () {
    return {
      // 文章评论相关数据
      offset: null, // 请求下一页数据的页码
      totalCount: 0, // 总数据条数
      // list: [],
      loading: false,
      finished: false
    }
  },
  computed: {},
  watch: {},
  created () {
    this.loading = true
    this.onLoad()
  },
  mounted () {},
  methods: {
    async onLoad () {
      console.log('我调用了')
      // 千万注意解构赋值拿出来的变量是内存新开辟的空间，如果想改变源对象的值必须要在源对象上修改不能解构重新赋值
      try {
        const { data } = await getComments({
          type: this.type, // 评论类型，a-对文章(article)的评论，c-对评论(comment)的回复
          source: this.source.toString(), // 源id，文章id或评论id
          offset: this.offset, // 获取评论数据的偏移量，值为评论id，表示从此id的数据向后取，不传表示从第一页开始读取数
          limit: 10
        })
        const { results } = data.data
        // console.log(this.articleComment.offset)
        console.log(results)
        this.list.push(...results)
        // this.articleComment.offset = (this.articleComment.offset + 1) + 1
        this.loading = false
        this.totalCount = data.data.total_count
        this.offset = data.data.last_id
        this.$emit('onload-success', data.data)
        // this.finished = true
        // 4. 判断是否还有数据
        if (results.length) {
          this.loading = false
        } else {
          this.finished = true
        }
      } catch (err) {
        this.loading = false
        this.finished = true
        this.$toast.fail('请求评论数据失败')
      }
      // this.loading = false
      // this.finished = true
      // if (this.list.length >= this.articleComment.totalCount) {
      //   this.finished = true
      // }
    }
  }
}
</script>

<style lang="less" scoped>

</style>
